package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.moraleducationbasic.MoralEducationBasicGetDto;
import com.teacher.pojo.entity.MoralEducationBasic;
import com.teacher.mapper.MoralEducationBasicMapper;
import com.teacher.pojo.entity.PerStudent;
import com.teacher.pojo.vo.moraleducationbaisc.MoralEducationBasicGetVo;
import com.teacher.service.IMoralEducationBasicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 德育基础分 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-08-05
 */
@Service
public class MoralEducationBasicServiceImpl extends ServiceImpl<MoralEducationBasicMapper, MoralEducationBasic> implements IMoralEducationBasicService {

    @Autowired
    private IPerStudentService iPerStudentService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 初始化基础德育分
     *
     * @param classId
     * @return
     */
    @Override
    public CommonResult<String> init(Long classId) {

        List<PerStudent> studentList = iPerStudentService.list(new QueryWrapper<PerStudent>().lambda()
                .eq(PerStudent::getClassId, classId));
        List<MoralEducationBasic> educationBasicList = new ArrayList<>();
        Long currentTermId = termMapper.getCurrentTerm();
        List<String> studentKeyList = studentList.stream().map(PerStudent::getStudentKey).collect(Collectors.toList());

        if (studentList.size() > 0) {
            // 删除原有的数据
            this.remove(new QueryWrapper<MoralEducationBasic>().lambda()
                    .eq(MoralEducationBasic::getTermId, currentTermId)
                    .in(MoralEducationBasic::getStudentKey, studentKeyList));
            for (PerStudent perStudent : studentList) {
                MoralEducationBasic moralEducationBasic = new MoralEducationBasic();
                moralEducationBasic.setTermId(currentTermId);
                moralEducationBasic.setDoCoreValue(true);
                moralEducationBasic.setFourPrinciple(true);
                moralEducationBasic.setDoMoral(true);
                moralEducationBasic.setStudentKey(perStudent.getStudentKey());
                educationBasicList.add(moralEducationBasic);
            }
            if (educationBasicList.size() > 0) {
                this.saveBatch(educationBasicList);
            }
        }

        return CommonResult.success("初始化成功");
    }

    /**
     * 查询德育基本分
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<MoralEducationBasicGetVo>> getPage(MoralEducationBasicGetDto dto) {
        Page<MoralEducationBasicGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        IPage<MoralEducationBasicGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }
}
